package com.wenx.schedule;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created by wenx on 2019-08-14 09:32
 */
public class Demo1 {

    public static void main(String[] args) {
        ScheduledExecutorService schedule = Executors.newScheduledThreadPool(1);
        Beeper b = new Demo1().new Beeper();
        schedule.scheduleAtFixedRate(b, 0, b.DELAY, b.TIME_UNIT);
    }



    class Beeper implements Runnable {

        private int times = 1;
        private int count = 0;
        public final int DELAY = 2;
        public final TimeUnit TIME_UNIT = TimeUnit.SECONDS;
        @Override
        public void run() {
            if(count == times) {
                DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                System.out.println(format.format(new Date())+ "  第 "+times+" 次运行，距离上次 "+(DELAY*times) +" 秒");
                count = 1;
                times++;
            }else {
                count++;
            }
            System.out.println("times = " + times + " count = " + count + " delay = " + DELAY);
        }
    }

}